import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard', affix: 'true' }
    }]
  },
  {
    path: '/sys',
    component: Layout,
    redirect: '/sys/user',
    name: 'sysManage',
    meta: { title: '系统管理', icon: 'el-icon-s-tools' },
    children: [
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/system/user'),
        meta: { title: '用户管理', icon: 'el-icon-user-solid' }
      },
      {
        path: 'admin',
        name: 'admin',
        component: () => import('@/views/system/admin'),
        meta: { title: '人员管理', icon: 'el-icon-user' }
      }
    ]
  },

  {
    path: '/text',
    component: Layout,
    redirect: '/text/article',
    name: 'textManage',
    meta: { title: '文稿管理', icon: 'el-icon-document' },
    children: [
      {
        path: 'article',
        name: 'article',
        component: () => import('@/views/text/article'),
        meta: { title: '新闻管理', icon: 'el-icon-reading' }
      },
      {
        path: 'notice',
        name: 'notice',
        component: () => import('@/views/text/notice'),
        meta: { title: '公告管理', icon: 'el-icon-notebook-1' }
      }
    ]
  },

  {
    path: '/comments',
    component: Layout,
    redirect: '/comments/comment',
    name: 'commentsManage',
    meta: { title: '评论管理', icon: 'el-icon-chat-dot-round' },
    children: [
      {
        path: 'comment',
        name: 'comment',
        component: () => import('@/views/comment/comment'),
        meta: { title: '评论管理', icon: 'el-icon-chat-line-round' }
      },
    ]
  },

  {
    path: '/booths',
    component: Layout,
    redirect: '/booths/booth',
    name: 'boothsManage',
    meta: { title: '摊位管理', icon: 'el-icon-data-analysis' },
    children: [
      {
        path: 'booth',
        name: 'booth',
        component: () => import('@/views/booth/booth'),
        meta: { title: '摊位审核', icon: 'el-icon-receiving' }
      },
      {
        path: 'boothReport',
        name: 'boothReport',
        component: () => import('@/views/booth/boothReport'),
        meta: { title: '摊位统计', icon: 'el-icon-s-data' }
      },
    ]
  },

  {
    path: '/contracts',
    component: Layout,
    redirect: '/contracts/contract',
    name: 'contractsManage',
    meta: { title: '合同管理', icon: 'el-icon-folder-opened' },
    children: [
      {
        path: 'contract',
        name: 'contract',
        component: () => import('@/views/contract/contract'),
        meta: { title: '合同管理', icon: 'el-icon-folder-opened' }
      },
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
